package org.boot.fast.core.api.service;


import org.boot.fast.core.api.mapper.DynamicSqlMapper;
import org.boot.fast.core.mate.DatabaseMetaService;
import org.boot.fast.core.mate.entity.TableStructure;
import org.boot.fast.dao.entity.FastApiConfigDO;
import org.boot.fast.util.ObjectUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static org.boot.fast.util.SqlParamUtil.convertParamMap;

@Service
public class DynamicSqlService {
    @Resource
    private DynamicSqlMapper dynamicMapper;

    @Resource
    private DatabaseMetaService metaService;

    /**
     * 执行动态查询SQL
     */
    public List<Map<String, Object>> executeQuery(FastApiConfigDO apiConfig, Map<String, Object> params) {
        try {
            return dynamicMapper.executeDynamicQuery(apiConfig, apiConfig.getSqlBody(), convertParamMap(params));
        } catch (Exception e) {
            throw new RuntimeException("executeQuery失败: " + e.getMessage());
        }
    }

    /**
     * 执行动态更新SQL
     */
    public int executeUpdate(FastApiConfigDO apiConfig, Map<String, Object> params) {
        try {
            return dynamicMapper.executeDynamicUpdate(apiConfig, apiConfig.getSqlBody(), convertParamMap(params));
        } catch (Exception e) {
            throw new RuntimeException("executeUpdate失败: " + e.getMessage());
        }
    }

    public int executeUpdateBatch(FastApiConfigDO apiConfig, List<Map<String, Object>> params) {
        try {
            return dynamicMapper.executeDynamicInsertBatch(apiConfig, apiConfig.getSqlBody(), convertParamMap(params));
        } catch (Exception e) {
            throw new RuntimeException("executeUpdate失败: " + e.getMessage());
        }
    }

    /**
     * 获取查询总数
     */
    public long executeCountQuery(FastApiConfigDO apiConfig, Map<String, Object> params) {
        try {
            return dynamicMapper.executeCountQuery(apiConfig, apiConfig.getSqlBody(), convertParamMap(params));
        } catch (Exception e) {
            throw new RuntimeException("executeCountQuery失败: " + e.getMessage());
        }
    }




    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

//    /**
//     * 执行动态查询SQL
//     */
//    public List<Map<String, Object>> executeQuery(String sql, Map<String, Object> params) {
//        try {
//            return dynamicMapper.executeDynamicQuery(sql, convertParamMap(params));
//        } catch (Exception e) {
//            throw new RuntimeException("executeQuery失败: " + e.getMessage());
//        }
//    }
//
//    /**
//     * 执行动态更新SQL
//     */
//    public int executeUpdate(String sql, Map<String, Object> params) {
//        try {
//            return dynamicMapper.executeDynamicUpdate(sql, convertParamMap(params));
//        } catch (Exception e) {
//            throw new RuntimeException("executeUpdate失败: " + e.getMessage());
//        }
//    }
//
//    /**
//     * 获取查询总数
//     */
//    public long executeCountQuery(String sql, Map<String, Object> params) {
//        try {
//            return dynamicMapper.executeCountQuery(sql, convertParamMap(params));
//        } catch (Exception e) {
//            throw new RuntimeException("executeCountQuery失败: " + e.getMessage());
//        }
//    }
}
